<?php
namespace App\Http\Controllers\Xcx;

use App\Http\Controllers\Controller;
use App\Service\Xcx\IndexService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    //码上农场
//    protected $app_id = 'wxbe82d7856be9d841';
//    protected $app_secret = '4c7f61fe2ddaa2aa1d7c5d62e850e002';
    //自由码码
    protected $app_id = 'wx2812bb4a4a3795a2';
    protected $app_secret = '90b204ac6418d1386fbf944bb146e4d7';

    //微信授权
    public function wxOne(Request $request)
    {
        $appid = $this->app_id; //env('WECHAT_APPID');
        $appsecret = $this->app_secret; //env('WECHAT_SECRET');
        $code = $request->post('code', '');
        if (!$code) {
            return $this->toJson(-2, 'CODE必须的');
        }
        $url = 'https://api.weixin.qq.com/sns/jscode2session';
        $url .= '?appid=' . $appid;
        $url .= '&secret=' . $appsecret;
        $url .= '&js_code=' . $code;
        $url .= '&grant_type=authorization_code';
        $resCurl = $this->wxCurl($url);
        //ddd($resCurl);
        if ($resCurl['code'] < 0) {
            return $this->toJson(-2, $resCurl['msg']);
        }
        $openid = $resCurl['data']['openid'];
        $res = IndexService::wxinfo($openid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //微信更新
    public function wxModify(Request $request)
    {
        $uid = $request->post('uid', 0);
        $pas['nick'] = $request->post('nick', '');
        $pas['head'] = $request->post('head', '');
        if (!$uid) {
            return $this->toJson(-2, 'UID必须的');
        }
        if (!$pas['nick']) {
            return $this->toJson(-2, '昵称必须的');
        }
        if (!$pas['head']) {
            return $this->toJson(-2, '头像必须的');
        }
        $res = IndexService::wxModify($uid, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //微信头像
    public function wxHead(Request $request)
    {
        $uid = $request->post('uid', 0);
        if (!$uid) {
            return $this->toJson(-2, 'UID必须的');
        }
//        ddd($_POST);
        $resUpload = Upload::uploadImg($request, 'head');
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        $wxhead = env('DOMAIN') . '/' .$resUpload['data'];
        $res = IndexService::wxHead($uid, $wxhead);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $wxhead);
    }

    /**
     * 构建CURL请求，单线程
     * @param $url 请求地址
     * @return array
     */
    public function wxCurl($url)
    {
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $output = curl_exec($curl);
        curl_close($curl);
        if (!$output) {
            return array('code'=>-1, 'msg'=>'请求失败');
        }
        $items = json_decode($output,true);
        return array('code' => 0, 'msg' => 'success', 'data' => $items);
    }

    //初始化
    public function init(Request $request)
    {
        $type = $request->post('type', 1);
        $res = IndexService::bomMenus($type);
        return $this->toJson(0, '', $res);
    }

    //type初始化
    public function typeInit(Request $request)
    {
        $res = IndexService::typeInit();
        return $this->toJson(0, '', $res);
    }

    //轮播图
    public function banner(Request $request)
    {
        $type = $request->post('type', 0);
        $res = IndexService::banner($type);
        return $this->toJson(0, '', $res);
    }

    //我的页面
    public function my(Request $request)
    {
        $uid = $request->post('uid', 0);
        $res = IndexService::my($uid);
        return $this->toJson(0, '', $res);
    }
}